package com.yhq.basic.nio;


import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author hqyin
 * @date 2024/6/7 11:53 AM
 */
public class TestBIO {
    public static void main(String[] args) throws IOException {
        //测试数组IO
        test_ByteArrayInputStream();
    }



    private static void test_ByteArrayInputStream() throws IOException {
        InputStream in = new ByteArrayInputStream("HelloWord".getBytes(StandardCharsets.UTF_8));
        //标记当前位置
        in.mark(2);
        byte[] bytes = new byte[2];
        for (int i = 0; i < bytes.length; i++) {
            int tmp;
            if ((tmp = in.read()) != -1) {
                bytes[i] = (byte) tmp;
            }
        }
        System.out.println(new String(bytes));

        //读取时候跳过几个位置
        in.skip(1);

        //标记当前位置
        in.mark(1);
        byte[] bytes2 = new byte[2];
        for (int i = 0; i < bytes2.length; i++) {
            int tmp;
            if ((tmp = in.read()) != -1) {
                bytes2[i] = (byte) tmp;
            }
        }
        System.out.println(new String(bytes2));
        //重置到最近的一次标记
        in.reset();
        byte[] bytes3 = new byte[2];
        for (int i = 0; i < bytes3.length; i++) {
            int tmp;
            if ((tmp = in.read()) != -1) {
                bytes3[i] = (byte) tmp;
            }
        }
        System.out.println(new String(bytes3));

        //读取时候跳过几个位置
        in.skip(1);
        //返回还有可读数据的长度
        System.out.println(in.available());


    }

}
